package com.xie.leetcode.string;

//17. 电话号码的字母组合
//        给定一个仅包含数字 2-9 的字符串，返回所有它能表示的字母组合。答案可以按 任意顺序 返回。
//
//        给出数字到字母的映射如下（与电话按键相同）。注意 1 不对应任何字母。
//
//        示例 1：
//
//        输入：digits = "23"
//        输出：["ad","ae","af","bd","be","bf","cd","ce","cf"]
//        示例 2：
//
//        输入：digits = ""
//        输出：[]
//        示例 3：
//
//        输入：digits = "2"
//        输出：["a","b","c"]
//
//
//        提示：
//
//        0 <= digits.length <= 4
//        digits[i] 是范围 ['2', '9'] 的一个数字。

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiezhendong
 * @date 2021/11/1
 */
public class LetterCombinations {


    public static void main(String[] args) {
        LetterCombinations letterCombinations = new LetterCombinations();
        System.out.println(letterCombinations.letterCombinations("23"));
        System.out.println(letterCombinations.letterCombinations("234"));
        System.out.println(letterCombinations.letterCombinations(""));
        System.out.println(letterCombinations.letterCombinations("2"));
    }

    static String number2 = "abc";
    static String number3 = "def";
    static String number4 = "ghi";
    static String number5 = "jkl";
    static String number6 = "mno";
    static String number7 = "pqrs";
    static String number8 = "tuv";
    static String number9 = "wxyz";

    static Map<Integer, String> map;

    static {
        map = new HashMap<>();
        map.put(2, number2);
        map.put(3, number3);
        map.put(4, number4);
        map.put(5, number5);
        map.put(6, number6);
        map.put(7, number7);
        map.put(8, number8);
        map.put(9, number9);
    }

    public List<String> letterCombinations(String digits) {
        List<String> strList = new ArrayList<>();
        if ("".equals(digits)) {
            return strList;
        }
        char[] digitsChars = digits.toCharArray();
        for (char digitsChar : digitsChars) {
            List<String> strOneList = new ArrayList<>();
            int value = Integer.valueOf(String.valueOf(digitsChar));
            String number = map.get(value);
            if (number != null) {
                if (strList.size() == 0) {
                    char[] numbers = number.toCharArray();
                    for (char c : numbers) {
                        strOneList.add(String.valueOf(c));
                    }
                } else {
                    char[] numbers = number.toCharArray();
                    for (String s : strList) {
                        for (char c : numbers) {
                            strOneList.add(s + String.valueOf(c));
                        }
                    }
                }
                strList.clear();
                strList.addAll(strOneList);
            }
        }
        return strList;
    }
}
